﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TWS.Client;

public partial class admin_ChiTietTinTuc : BaseAdminPage
{
    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        //Page.Validate();
        //if (!Page.IsValid) return;
        //TinTucHelper spHP = new TinTucHelper();
        //TinTucDataSet spDS = new TinTucDataSet();
        //TinTucDataSet.TinTucRow TinTucmoi = spDS.TinTuc.NewTinTucRow();
        //TinTucmoi.TenTinTuc = txtTenTinTuc.Text;
        //TinTucmoi.LoaiTinTuc = Convert.ToInt32(ddlLoaiCon.SelectedValue);
        //TinTucmoi.HangSanXuat = txtHangSanXuat.Text;
        //TinTucmoi.ChungLoai = txtChungLoai.Text;
        //TinTucmoi.Gia = Convert.ToInt32(txtGia.Text);
        //////UploadImage();
        //if (txtHinhAnh.Text == string.Empty)
        //{
        //    txtHinhAnh.Text = "noimage.jpg";
        //}
        //TinTucmoi.HinhAnh = txtHinhAnh.Text;
        //TinTucmoi.TinTucMoi = ckTinTucMoi.Checked;
        //TinTucmoi.ThuTu = Convert.ToInt32(txtThuTu.Text);
        //TinTucmoi.NgayDang = DateTime.Now;

        //TinTucmoi.TongQuan = txtTongQuan.Text;
        //TinTucmoi.ThongSoKyThuat = txtThongSo.Text;
        //TinTucmoi.Album = txtAlbum.Text;
        //spDS.TinTuc.AddTinTucRow(TinTucmoi);
        //spHP.SaveTinTuc(spDS);
        //RedirectPage(Request.RawUrl, true);
        SaveData();
    }

    private TinTucDataSet m_TinTucDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                string mode = Request.QueryString["mode"];
                bool isNew = false;
                if (mode != "edit")
                {
                    isNew = true;
                }
                string key = Request.QueryString["id"];
                if (!isNew)
                {
                    if (key != null && key != string.Empty)
                    {
                        LoadData(key);
                    }
                }
                else
                {
                    m_TinTucDataSet = new TinTucDataSet();

                    TinTucDataSet.TinTucRow danhMuc = m_TinTucDataSet.TinTuc.NewTinTucRow();
                    danhMuc.TinTucID = -1;

                    m_TinTucDataSet.TinTuc.AddTinTucRow(danhMuc);
                    Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"] = m_TinTucDataSet;
                }

            }
            catch (Exception ex)
            {
                //DisplayMesseage(ex.Message);
                this.RedirectPage("pgError.aspx", false);
            }
        }
    }

    private void LoadData(string benhLyId)
    {
        int itemId;
        if (int.TryParse(benhLyId, out itemId))
        {
            TinTucHelper helper = new TinTucHelper();
            TinTucDataSet tempDs = helper.LoadTinTuc(itemId);


            TinTucDataSet.TinTucRow danhMuc = tempDs.TinTuc.FindByTinTucID(itemId);
            m_TinTucDataSet = new TinTucDataSet();
            m_TinTucDataSet.TinTuc.ImportRow(danhMuc);

            Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"] = m_TinTucDataSet;

            txtTenSanPham.Text = danhMuc.TieuDe;
            lbTenSanPham.Text = danhMuc.TieuDe;
            if (!danhMuc.IsNoiDungNull()) txtNoiDung.Text = danhMuc.NoiDung;

            //}
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.RedirectLastVisitedPage();
    }

    private void SaveData()
    {
        try
        {
            string mode = Request.QueryString["mode"];
            m_TinTucDataSet = (TinTucDataSet)Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"];
            //SetViewMode(canEdit);
            TinTucDataSet.TinTucRow danhMuc = m_TinTucDataSet.TinTuc[0];

            danhMuc.TieuDe = txtTenSanPham.Text;
            danhMuc.NoiDung = txtNoiDung.Text;


            TinTucHelper helper = new TinTucHelper();
            helper.SaveTinTuc(m_TinTucDataSet);
        }
        catch (Exception ex)
        {
            //DisplayMesseage(ex.Message);
            this.RedirectPage("pgError.aspx", false);
        }
        this.RedirectPage("QuanLyTinTuc.aspx", false);
    }
}